package com.leo.utils;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

/**
 * Created by leo on 16/4/25.
 *
 * 类型转换
 */
public class TypeUtils {

    /**
     *  字符串转换为int ,异常则返回最小值
     * @param str
     * @return
     */
    public static int toInt(String str){
        int result = 0;
        try{
            result = Integer.valueOf(str);
        }catch(Exception ex){
            result = Integer.MIN_VALUE;
        }
        return result;
    }

    /** 字符串转换int,异常则返回默认值
     *
     * @param str
     * @param defaultValue
     * @return
     */
    public static int toInt(String str, int defaultValue){
        int result = 0;
        try{
            result = Integer.valueOf(str);
        }catch(Exception ex){
            result = defaultValue;
        }
        return result;
    }

    /**
     *  字符串转换为long ,异常则返回最小值
     * @param str
     * @return
     */
    public static long toLong(String str){
        long result = 0;
        try{
            result = Long.valueOf(str);
        }catch(Exception ex){
            result = Long.MIN_VALUE;
        }
        return result;
    }

    /** 字符串转换为float,异常则返回最小值
     *
     * @param str
     * @return
     */
    public static float toFloat(String str){
        float result = 0;
        try{
            result = Float.valueOf(str);
        }catch(Exception ex){
            result = Float.MIN_VALUE;
        }
        return result;
    }

    /** 字符串转换为double ,异常则返回最小值
     *
     * @param str
     * @return
     */
    public static double toDouble(String str){
        double result = 0;
        try{
            result = Double.valueOf(str);
        }catch(Exception ex){
            result = Double.MIN_VALUE;
        }
        return result;
    }

    /**
     *  float 转换为 int
     * @param f
     * @return
     */
    public static int float2Int(float f){
        return Math.round(f);
    }

    /**  double转换整数
     *
     * @param d
     * @return
     */
    public static int double2Int(double d){
        return Long.valueOf(Math.round(d)).intValue();
    }

    /** collect2String(包含list, set)
     *
     * @param obj
     * @return
     */
    public static String collect2String(Object obj){
        String result = "";
        if (CheckUtils.isNotEmpty(obj)){
            if (obj instanceof Collection){
                Object[] objArray = ((Collection<?>)obj).toArray();
                for(Object tempObj : objArray){
                    if (CheckUtils.isNotEmpty(tempObj)){
                        result += "," + tempObj.toString();
                    }
                }
                if (result.length() > 0){
                    result = result.substring(1);
                }
            }else{
                result = obj.toString();
            }
        }
        return result;
    }

    /**
     *  数组转换成list
     * @param array
     * @param <T>
     * @return
     */
    public static <T extends Object> List<T> array2List(T[] array){
        List<T> result = new ArrayList<T>();
        if (!CheckUtils.isEmpty(array)){
            for(T entity : array){
                result.add(entity);
            }
        }
        return result;
    }

}
